from flask import Flask, request, render_template, send_file
from solve_maze_problem import getOutMaze
from os import getcwd, path

app = Flask(__name__)

# 获取当前工作目录路径
current_directory = getcwd()


@app.route("/")
def main():
    return render_template("index.html")


@app.route("/cheated", methods=["POST"])
def cheated():
    data = request.json
    maze, solution = data["maze"], data["solution"]
    attempt_path, target_path = getOutMaze(maze, 0, 0, 16, 16, solution)
    if solution == "BFS" or solution == "A*":
        target_path.reverse()
    return {"attempt_path": attempt_path, "target_path": target_path}


@app.route("/get_image")
def get_image():
    # 从文件系统中读取图片
    image_path = path.join(current_directory, "static", "ji.png")
    # 返回图片字节流给前端
    return send_file(image_path, mimetype="image/jpeg")


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=1314)
